package com.onefish.toolkit.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.onefish.toolkit.core.ThreadHelper;
import com.onefish.toolkit.entity.ThreadTemplate;
import com.onefish.toolkit.utils.ReturnT;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.lang.management.ThreadInfo;
import java.util.*;
import java.util.regex.Pattern;

@Service
public class ThreadService {
    private static final Logger logger = LoggerFactory.getLogger(ThreadService.class);

    public String cpuLoadTopNThreads(String processId, Integer n) {
        try {
            //进程id合法性校验
//            if (StringUtils.isBlank(processId)) {
//                return new ReturnT<>(ReturnT.ERROR_CODE, "进程id为空！");
//            }
//            Pattern pattern = Pattern.compile("[0-9]*");
//            boolean isProcess = pattern.matcher(processId).matches();
//            if (!isProcess) {
//                return new ReturnT<>(ReturnT.ERROR_CODE, "进程id格式不正确！");
//            }
//            ThreadHelper threadHelper = new ThreadHelper();
//            threadHelper.sample(threadHelper.getThreads());
//            threadHelper.pause(ThreadHelper.sampleInterval);
//            List<ThreadTemplate> threadTemplates = threadHelper.sample(threadHelper.getThreads());
//            if (CollectionUtils.isEmpty(threadTemplates)) {
//                return new ReturnT<>(ReturnT.ERROR_CODE, "抽样获取的线程信息为空！");
//            }
//            //统计cpu负载高的前n个线程，如果n不传则默认显示前5个
//            Integer topN = n;
//            if (topN == null || topN == 0) {
//                topN = ThreadHelper.topNLoad;
//            }
//            List<ThreadTemplate> topNThreadTemplates = threadTemplates.subList(0, topN);
//            List<Long> tids = new ArrayList<>();
//            for (ThreadTemplate thread : topNThreadTemplates) {
//                if (thread.getId() > 0) {
//                    tids.add(thread.getId());
//                }
//            }
//            if (CollectionUtils.isEmpty(tids)) {
//                return new ReturnT<>(ReturnT.ERROR_CODE, "不存在线程id>0的cpu高负载的线程！");
//            }
//            ThreadInfo[] threadInfos = ThreadHelper.threadMXBean.getThreadInfo(ThreadHelper.toPrimitive(tids.toArray(new Long[0])), false, false);
//            if (threadInfos == null || threadInfos.length == 0) {
//                return new ReturnT<>(ReturnT.ERROR_CODE, "获取抽样线程信息为空！");
//            }
//            List<ThreadTemplate> topNThreads = new ArrayList<>();
//            JSONArray jsonArray = new JSONArray();
//            for (ThreadTemplate thread : topNThreadTemplates) {
//                ThreadInfo threadInfo = threadHelper.findThreadInfoById(threadInfos, thread.getId());
//                //未查询线程信息
//                if (threadInfo == null) {
//                    continue;
//                }
//                String stacktrace = ThreadHelper.getFullStacktrace(threadInfo, thread.getCpu(), thread.getDeltaTime(), thread.getTime(), 0, 0);
//                thread.setThreadInfo(threadInfo);
//                JSONObject jsonObject = new JSONObject();
////                jsonObject.put("stacktrace", "\n            hello\n            world");
//                jsonObject.put("c","123");
//                jsonArray.add(jsonObject);
//                topNThreads.add(thread);
//            }
//            if (CollectionUtils.isEmpty(topNThreads)) {
//                return new ReturnT<>(ReturnT.ERROR_CODE, "获取负载高的抽样线程信息为空！");
//            }
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < 3; i++) {
                JSONObject jsonObject = new JSONObject();
//                jsonObject.put("stacktrace", "\n            hello\n            world");
                jsonObject.put("c","123");
                jsonArray.add(jsonObject);
            }
            String content = JSON.toJSONString(jsonArray,true);
            logger.info(content);
            System.out.println(content);
            return content;
        } catch (Exception e) {
            logger.error("cpuLoadTopNThreads error", e);
            return "";
//            return new ReturnT<>(ReturnT.ERROR_CODE, "cpuLoadTopNThreads error: " + e.getMessage());
        }
    }
}
